<?php
namespace app\appoint\model;
use app\BaseModel;
use think\facade\Db;



class AdminProject extends BaseModel
{
    //定义表名
    protected $name = 'lb_appoint_project';

    protected $append = [
        'cate_text',
        'days_text',
        'range_text',
        'carousel_text',
    ];

    /**
     * @param $value
     * @param $data
     * @return array
     * 分类
     */
    public function getCateTextAttr($value, $data){
        $cate = Db::name('lb_appoint_classify')->where(['id'=>$data['classify_id']])->value('title');
        return $cate;
    }
    /**
     * @param $value
     * @param $data
     * @return array
     * 星期
     */
    public function getDaysTextAttr($value, $data){
        if(!empty($data['days'])){
            $days = explode(',',$data['days']);
            foreach ($days as &$v){
                $v = $this->chanageWeek($v);
            }
            return $days;
        }
    }

    /**
     * User: chenniang
     * Date: 2019-09-26 15:15
     * @param $value
     * @return string
     * descption:转富文本
     */
    public function getContentAttr($value){
        return html_entity_decode($value);
    }
    /**
     * @param $value
     * @return array
     * 转换天数数组
     */
    public function getDaysAttr($value){
        if(!empty($value)){
            $value = explode(',',$value);
            foreach ($value as &$v){
                $v = intval($v);
            }
        }
        return $value;
    }

    /**
     * @param $value
     * @param $data
     * @return array
     * 拼接时间
     */
    public function getRangeTextAttr($value, $data){
        $start = explode(',',$data['times_start']);
        $end   = explode(',',$data['times_end']);
        if(!empty($start)){
            foreach ($start as $key=>$value){
                $date[] = $value.'-'.$end[$key];
            }
            return $date;
        }
    }

    /**
     * @param $day
     * 转换星期
     * @return array
     */
    public function chanageWeek($day){
        switch ($day) {
            case 1:
                $list = '星期一';
                break;
            case 2:
                $list = '星期二';
                break;
            case 3:
                $list = '星期三';
                break;
            case 4:
                $list = '星期四';
                break;
            case 5:
                $list = '星期五';
                break;
            case 6:
                $list = '星期六';
                break;
            case 7:
                $list = '星期天';
                break;
            default :
                $list = '';
                break;
        }
        return $list;
    }

    /**
     * @param $vaule
     * @return mixed|string
     * 转换封面图
     */
    public function getCoverAttr($vaule){
        $cover = transImagesOne([$vaule],['cover']);
        return $cover;
    }
    /**
     * @param $vaule
     * @param $data
     * @return mixed
     * 转换轮播图
     */
    public function getCarouselTextAttr($vaule,$data){
        $data = transImages($data,['carousel']);
        return !empty($data['carousel'])?$data['carousel']:[];
    }
    /**
     * @param $vaule
     * @return int
     * 转换数字类型
     */
    public function getTimeStartAttr($vaule){

        return intval($vaule);
    }
    /**
     * @param $vaule
     * @return int
     * 转换数字类型
     */
    public function getTimeRangeAttr($vaule){

        return intval($vaule);
    }
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }
    /**
     * @param $dis
     * @return array
     * 预约服务列表
     */
    public function proList($dis,$page = 10){
        $data = $this->where($dis)->status(-1)->order('top desc')->paginate($page)->toArray();
        return $data;
    }

    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 预约分类
     */
    public function cateSelect($dis){
        $data = $this->where($dis)->status(-1)->order('top desc')->select()->toArray();
        return $data;
    }
    /**
     * @param $data
     * @return int|string
     * 添加预约服务
     */
    public function proAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑预约服务
     */
    public function proUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * @param $dis
     * @return array
     * 预约服务详情
     */
    public function proInfo($dis){
        $data = $this->where($dis)->status(-1)->find();
        return !empty($data)?$data->toArray():$data;
    }



}